﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;


/// <summary>
/// SQ_Eval 的摘要说明
/// </summary>
public class SQ_Eval
{
    DBHelper db = new DBHelper();
    private DateTime NowKq;
    private string kq_s_d;
    private string kq_e_d;

    public int kp_year = 0;
    public int kp_month = 0;
    public int now_year = 0;
    public int now_month = 0;
    public int last_year = 0;
    public int last_month = 0;
    private string now_kq_s_d;
    private string now_kq_e_d;

    public SQ_Eval()
    {
        string sql = "select * from A_Eval_Sys where [state]=0";
        DataTable dt = db.getList(sql);
        int year = Convert.ToInt32(dt.Rows[0]["year"].ToString());
        kp_year = year;
        int month = Convert.ToInt32(dt.Rows[0]["month"].ToString());
        kp_month = month;

        NowKq = Convert.ToDateTime(year + "-" + month + "-01");
        kq_s_d = dt.Rows[0]["rq_start"].ToString();
        kq_e_d = dt.Rows[0]["rq_end"].ToString();

        DateTime now = DateTime.Now;
        now_year = now.Year;
        now_month = now.Month;
        last_year = now.AddMonths(-1).Year;
        last_month = now.AddMonths(-1).Month;
        now_kq_s_d = kq_s_d;
        now_kq_e_d = kq_e_d;

    }


    public void StartEval(string sign)
    {

        //计算完毕之后记得写入统计
        switch (sign)
        {
            case "0":
                return;
                SetEvalMain();
                SetGdTask();
                SetTaskEvalGp();
                SetSpecial();
                SetZZZZ();
                string sql = "insert into A_Eval_Log (type,logtxt) values('0',N'自动计算考评')";
                db.executeNoneQuery(sql);
                break;
            case "1":
                SetEvalMain();
                break;
            case "2":
                SetGdTask();
                break;
            case "4":
                SetTaskEvalGp();
                break;
            case "5":
                SetSpecial();
                break;
            case "6":
                SetZZZZ();
                break;
        }
    }

    #region 自动生成当月考评总表及考评表 
    /// <summary>
    /// (一)自动生成当月考评总表及考评表
    /// </summary>
    private void SetEvalMain()
    {
        DateTime start_day = Convert.ToDateTime(now_kq_s_d);
        DateTime end_day = Convert.ToDateTime(now_kq_e_d);
        string sql = "";
        sql = "select * from dict_dept_position where isUsing=1 and ((LastYear<" + now_year + ") or(LastYear=" + now_year + " and  LastMonth<" + now_month + "))";
        DataTable dt = db.getList(sql);
        int ii = 0;
        foreach (DataRow dr in dt.Rows)
        {

            int task_zx_p = 1000;
            if (dr["type"].ToString() == "1")
            {
                task_zx_p = 0;
            }
            //这里需要调用两个方法 写入总表 写入考评总表 
            string Psid = dr["id"].ToString();
            string UserBh = dr["userbh"].ToString();
            string Evalid = dr["evalid"].ToString();
            int Year = now_year;
            int Month = now_month;
            sql = "insert into A_Eval_Main (psid,userbh,year,month,task_zx_p) values ('" + Psid + "','" + UserBh + "','" + Year + "','" + Month + "'," + task_zx_p + ")";
            db.executeNoneQuery(sql);
            sql = "update dict_dept_position set LastYear=" + now_year + ",LastMonth=" + now_month + " where id=" + dr["id"].ToString();
            db.executeNoneQuery(sql);
            ii++;
        }
        sql = "insert into A_Eval_Log(type,logtxt) values(1,N'成功写入" + now_year + "年" + now_month + "月统计记录" + ii + "条')";
        db.executeNoneQuery(sql);

    }

    #endregion

    #region 自动生成当月固定任务

    /// <summary>
    /// 自动生成当月固定任务
    /// </summary>
    private void SetGdTask()
    {
        DateTime start_day = Convert.ToDateTime(now_kq_s_d);
        DateTime end_day = Convert.ToDateTime(now_kq_e_d);
        DateTime mid_day = start_day;

        string sql = "";
        sql = "select * from dict_dept_position_Task where isUsing=1 and ((Year<" + kp_year + ") or(Year=" + kp_year + " and  Month<" + kp_month + "))";
        DataTable dt = db.getList(sql);
        int ii = 0;
        foreach (DataRow dr in dt.Rows)
        {
            int FP_PL = Convert.ToInt32(dr["FP_PL"].ToString());
            if (FP_PL == 2 && (kp_month != 3 || kp_month != 6 || kp_month != 9 || kp_month != 12))//季度分配
            {
                continue;
            }
            else if (FP_PL == 3 && (kp_month != 6 || kp_month != 12))//半年分配
            {
                continue;
            }
            else if (FP_PL == 4 && kp_month != 12)//半年分配
            {
                continue;
            }

            string id = dr["id"].ToString();
            int sc_d = Convert.ToInt16(dr["sc_d"].ToString());
            if (sc_d > 0 && sc_d <= 28)
            {
                mid_day = start_day.AddDays(sc_d);
            }
            else
            {
                mid_day = end_day;
            }
            sql = "insert into A_Task(type,title,detail,Tdata,Bm_Kh,Ry_Kh,Bm_Bkh,Ry_Bkh,LX_sj,LX_kh,rq_yjs,Year,Month,iszd,LX_fd,RealId,FZ,LX_TYPE) values(";
            sql += "0,N'" + dr["title"].ToString() + "',N'" + dr["detail"].ToString() + "','" + dr["Tdata"].ToString() + "','" + dr["Bm_Kh"].ToString() + "','" + dr["Ry_Kh"].ToString() + "','" + dr["Bm_Bkh"].ToString() + "','" + dr["Ry_Bkh"].ToString() + "','" + dr["LX_sj"].ToString() + "','" + dr["LX_kh"].ToString() + "',N'" + mid_day.ToString("yyyy-MM-dd") + "','" + kp_year + "','" + kp_month + "','" + dr["iszd"].ToString() + "','" + dr["LX_fd"].ToString() + "'," + id + "," + dr["FZ"].ToString() + "," + dr["LX_TYPE"].ToString() + ")";
            int i = db.executeNoneQuery(sql);
            if (i == 1)
            {
                sql = "update dict_dept_position_Task set Year=" + kp_year + ",Month=" + kp_month + " where id=" + id;
                db.executeNoneQuery(sql);
                ii++;
            }

        }

        sql = "insert into A_Eval_Log(type,logtxt) values(2,N'成功写入" + kp_year + "年" + kp_month + "月固定任务" + ii + "条')";
        db.executeNoneQuery(sql);
    }
    #endregion

    #region 计算任务分数
    /// <summary>
    /// 计算自动完成任务
    /// </summary>
    public string SetZdTaskPoint(string taskid)
    {
        string sql = "select * from A_Task where id=" + taskid;
        DataRow dr = db.getList(sql).Rows[0];

        int LX_TYPE = Convert.ToInt32(dr["LX_TYPE"].ToString());
        int LX_fd = Convert.ToInt32(dr["LX_fd"].ToString());
        double Pdata = Convert.ToDouble(dr["Pdata"].ToString());
        double Tdata = Convert.ToDouble(dr["Tdata"].ToString());
        bool task_state = Convert.ToBoolean(dr["task_state"].ToString());
        double FZ = Convert.ToDouble(dr["FZ"].ToString());
        double S_FZ = 0;
        double mid = 0;
        string retu_str = "";
        switch (LX_fd)
        {
            case 0: //只减不加
                if (task_state == true)
                {
                    S_FZ = 0;
                }
                else
                {
                    S_FZ = 0 - FZ;
                }
                retu_str = "任务已验收,本任务扣分" + S_FZ + "分";
                break;
            case 2: //纯加分
                if (task_state == true)
                {
                    S_FZ = FZ;
                }
                else
                {
                    S_FZ = 0;
                }
                retu_str = "任务已验收,本任务加分" + S_FZ + "分";
                break;
            case 1: //浮动价减
                if (Tdata == 0)
                {
                    S_FZ = 0;
                }
                else
                {
                    if (LX_TYPE == 0)  //首要任务
                    {
                        mid = Pdata / Tdata;
                        S_FZ = mid * FZ;
                        S_FZ = Math.Round(S_FZ, 2);
                    }
                    else
                    {
                        mid = Pdata - Tdata;
                        mid = mid / Tdata;
                        S_FZ = mid * FZ;
                        S_FZ = Math.Round(S_FZ, 2);
                    }
                }
                retu_str = "任务已验收,本任务最终得分" + S_FZ + "分";
                break;
            case 3: //浮动
                if (Tdata == 0)
                {
                    S_FZ = 0;
                }
                else
                {
                    mid = (Pdata - Tdata) / Tdata;
                    S_FZ = mid * FZ;
                    if (S_FZ > 0)
                    {
                        S_FZ = 0;
                    }
                    S_FZ = Math.Round(S_FZ, 2);
                }
                retu_str = "任务已验收,本任务最终得分" + S_FZ + "分";
                break;
        }

        try
        {
            sql = "UPDATE A_Task set S_FZ=" + S_FZ + " where id=" + taskid;
            db.executeNoneQuery(sql);

        }
        catch (Exception ex)
        {

        }
        return retu_str;

    }
    #endregion

    #region 计算当月考评任务得分
    /// <summary>
    /// 计算当月考评任务得分
    /// </summary>
    private void SetTaskEvalGp()
    {
        string sql = "select * from dict_dept_position where isUsing=1";
        DataTable dt = db.getList(sql);
        int ii = 0;
        int task_zx_s_0 = 0;
        int task_zx_s_1 = 0;
        int task_zx_s_2 = 0;
        int task_zx_p_0 = 0;
        int task_zx_p_1 = 0;
        int task_zx_p_2 = 0;
        int task_zx_p = 0;


        foreach (DataRow dr in dt.Rows)
        {
            string midstr = "";
            string userbh = dr["userbh"].ToString();
            string psid = dr["id"].ToString();

            //年度任务数
            task_zx_s_0 = db.getCount("v_A_Task", "Ry_bKh='" + psid + "' and year = '" + kp_year + "' and month = '" + kp_month + "' and type=0");


            //年度任务得分
            midstr = "select ISNULL(sum(S_FZ),0) as S_FZ from v_A_Task where Ry_BKh='" + psid + "'  and year = '" + kp_year + "' and month = '" + kp_month + "' and type=0";
            midstr = db.getList(midstr).Rows[0][0].ToString();
            task_zx_p_0 = Convert.ToInt32(Convert.ToDouble(midstr));

            //月度任务数
            task_zx_s_1 = db.getCount("v_A_Task", "Ry_BKh='" + psid + "' and year = '" + kp_year + "' and month = '" + kp_month + "' and type=1");

            //月度任务得分
            midstr = "select ISNULL(sum(S_FZ),0) as S_FZ from v_A_Task where Ry_Bkh='" + psid + "'  and year = '" + kp_year + "' and month = '" + kp_month + "' and type=1";
            task_zx_p_1 = Convert.ToInt16(Convert.ToDouble(db.getList(midstr).Rows[0][0].ToString()));

            //临时任务数
            task_zx_s_2 = db.getCount("v_A_Task", "Ry_BKh='" + psid + "' and year = '" + kp_year + "' and month = '" + kp_month + "' and type=2");

            //临时任务得分
            midstr = "select ISNULL(sum(S_FZ),0) as S_FZ from v_A_Task where Ry_BKh='" + psid + "'  and year = '" + kp_year + "' and month = '" + kp_month + "' and type=2";
            task_zx_p_2 = Convert.ToInt16(Convert.ToDouble(db.getList(midstr).Rows[0][0].ToString()));

            //总得分
            task_zx_p = task_zx_p_0 + task_zx_p_1 + task_zx_p_2;


            sql = "update A_Eval_Main set task_zx_s_0=" + task_zx_s_0 + ",task_zx_p_0=" + task_zx_p_0 + ",task_zx_s_1=" + task_zx_s_1 + ",task_zx_p_1=" + task_zx_p_1 + ",task_zx_s_2=" + task_zx_s_2 + ",task_zx_p_2=" + task_zx_p_2 + ",task_zx_p=" + task_zx_p + ",sj=getdate() where psid=" + psid + " and userbh='" + userbh + "' and year=" + kp_year + " and month=" + kp_month;
            ii = ii + db.executeNoneQuery(sql);


        }
        // sql = "insert into A_Eval_Log(type,logtxt) values(4,N'成功计算" + kp_year + "年" + kp_month + "月任务得分" + ii + "条')";
        // db.executeNoneQuery(sql);
    }
    #endregion

    #region 特殊加减分
    /// <summary>
    /// 计算特殊扣分
    /// </summary>
    private void SetSpecial()
    {
        string sql = "update A_Special_Eval set fz=(0-fz) where  isUsing=1 and [year]= " + kp_year + " and [month] = " + kp_month + "  and fz>0 and IsAdd=0";
        db.executeNoneQuery(sql);

        sql = "select sum(fz) as fz,psid from A_Special_Eval where  isUsing=1 and [year]= " + kp_year + " and [month] = " + kp_month + " group by psid";
        DataTable dt = db.getList(sql);
        int ii = 0;
        foreach (DataRow dr in dt.Rows)
        {
            string psid = dr["psid"].ToString();
            string fz = dr["fz"].ToString();
            sql = "update A_Eval_Main set special=" + fz + " where psid='" + psid + "' and year=" + kp_year + " and month=" + kp_month;
            db.executeNoneQuery(sql);
            ii++;
        }
        sql = "insert into A_Eval_Log(type,logtxt) values(5,N'成功计算" + kp_year + "年" + kp_month + "月特殊考评" + ii + "条')";
        db.executeNoneQuery(sql);
    }
    #endregion



    #region 最终得分
    /// <summary>
    /// 最终得分
    /// </summary>
    private void SetZZZZ()
    {
        string sql = "select * from [dbo].[v_A_Eval_Main] where year = '" + kp_year + "' and month = '" + kp_month + "' order by dept,parentid";
        DataTable dt = db.getList(sql);

        double dept_fz = 0;
        double gw_fz = 0;
        double gw_bl = 0;
        string tsql = "";
        foreach (DataRow dr in dt.Rows)
        {
            string parentid = dr["parentid"].ToString();
            if (parentid == "0")
            {
                dept_fz = Convert.ToDouble(dr["task_zx_p"].ToString());
                tsql = " update A_Eval_Main set task_zx_p_zz = (" + dept_fz + " + special+100),sj=getdate() where id=" + dr["id"].ToString();
                db.executeNoneQuery(tsql);
            }
            else
            {
                gw_fz = Convert.ToDouble(dr["task_zx_p"].ToString());//岗位自己得分
                gw_bl = Convert.ToDouble(dr["bl"].ToString());//岗位比例
                gw_fz = (dept_fz * gw_bl / 100);//按照比例折合分值
                tsql = " update A_Eval_Main set task_zx_p_zz = (" + gw_fz + " + special+100),sj=getdate() where id=" + dr["id"].ToString();
                db.executeNoneQuery(tsql);
            }


        }

    }
    #endregion
}
